package com.nip.workmanager.activity;

import android.R.menu;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.nip.workmanagement.R;
import com.nip.workmanager.common.ConstantIntentCode;
import com.nip.workmanager.common.utilities.Validator;
import com.nip.workmanager.database.CheckListTable;
import com.nip.workmanager.entity.CheckListEntity;

public class AddCheckListActivity extends Activity {

	private Button _btnChooseIcon;
	private Button _btnAddCheckList;
	private Button _btnBack;
	private int _resourceImage = 0;
	private CheckListTable _checkListTable;
	private EditText _edWorkName;
	private EditText _edWorkDes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_work_activity);
		Initialize();
	}

	/**
	 * Initialize local variables of class
	 */
	private void Initialize() {
		_btnChooseIcon = (Button) findViewById(R.id.btnChooseIcon);
		_btnAddCheckList = (Button) findViewById(R.id.btnAdd);
		_btnBack = (Button) findViewById(R.id.btnAddWorkBack);
		_edWorkName = (EditText) findViewById(R.id.edWorkName);
		_edWorkDes = (EditText) findViewById(R.id.edWorkDescription);
		_edWorkName.setFocusable(true);
		_btnChooseIcon.setOnClickListener(new MyEvent());
		_btnAddCheckList.setOnClickListener(new MyEvent());
		_btnBack.setOnClickListener(new MyEvent());

		_checkListTable = new CheckListTable(this);
	}

	/**
	 * View activity choose icon
	 */
	private void ShowChooseIconActivity() {
		Intent i = new Intent(AddCheckListActivity.this,
				ChooseIconCheckListActivity.class);
		startActivityForResult(i, ConstantIntentCode.CHOOSE_ICON_REQUEST);
	}

	/**
	 * Add a work to checklist
	 */
	private void AddWorkCheckList() {
		if (CheckValidate()) {
			CheckListEntity work = new CheckListEntity(null, _edWorkName
					.getText().toString(), _resourceImage , _edWorkDes
					.getText().toString());
			if(_checkListTable.InsertWorkToCheckList(work))
			{
				Toast.makeText(getApplication(),"Insert success !" , Toast.LENGTH_LONG).show();
				finish();
			}else
			{
				Toast.makeText(getApplication(),"Insert fail !" , Toast.LENGTH_LONG).show();
			}
		}
	}

	/**
	 * contain event of class
	 * 
	 * @author ThangNM
	 * 
	 */
	private class MyEvent implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.btnChooseIcon:
				ShowChooseIconActivity();
				break;
			case R.id.btnAdd:
				AddWorkCheckList();
				break;
			case R.id.btnAddWorkBack:
				onBackPressed();
				break;
			default:
				break;
			}
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == ConstantIntentCode.CHOOSE_ICON_REQUEST) {
			switch (resultCode) {
			case ConstantIntentCode.CHOOSE_ICON_RESULT:
				_resourceImage = data.getIntExtra(
						ConstantIntentCode.CHOOSE_ICON_ID, 0);
				break;

			default:
				break;
			}
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}

	/**
	 * Check validate
	 * 
	 * @return
	 */
	private Boolean CheckValidate() {
		AlertDialog.Builder messageBox = new AlertDialog.Builder(
				AddCheckListActivity.this);
		messageBox.setTitle("NIPTeam");
		messageBox.setMessage("Work name is not null");
		messageBox.setPositiveButton("OK",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						_edWorkName.requestFocus();
						dialog.cancel();
					}
				});

		if (!Validator.CheckNullOrEmpty(_edWorkName.getText().toString())) {
			messageBox.show();
			return false;
		}
		return true;
	}
}
